package cirno.open.api.config;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@ConfigurationProperties(prefix = "codesandbox.config")
@Data
public class DockerConfig {
    private String remoteHost;

    /**
     * 代码沙箱的镜像，Dockerfile 构建的镜像名，默认为 codesandbox:latest
     */
    private String image;

    /**
     * 内存限制，单位为字节，默认为 1024 * 1024 * 256 MB
     */
    private long memoryLimit;

    private long memorySwap;

    /**
     * 最大可消耗的 cpu 数
     */
    private long cpuCount;

    /**
     * 运行时间限制
     */
    private long timeoutLimit;

    private TimeUnit timeUnit;

    @Bean
    public DockerClient dockerClient() {
        return DockerClientBuilder.getInstance(DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost(remoteHost).build()).build();
    }
}
